<?php

/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2017/6/28
 * Time: 10:46
 */

namespace Admin\Controller;

use Admin\Service\QaService;

class QaController extends BaseController {


    public function __construct() {
        parent::__construct();
        $this->qaService = new QaService();
        $this->assign('q_status',C('QUESTION_STATUS'));
        $this->assign('a_status',C('ANSWER_STATUS'));
    }

    public function index() {
        $cat_ids = M('categories')->getField('id,cat_name');
        $this->assign('cat_ids',$cat_ids);
        $this->display();
    }
    public function ajax_index() {

        $nowPage = I('get.p', 1);
        $cat_ids = I('post.cat_ids', '');
        $title = I('post.title', '');
        $user_name = I('post.username', '');
        $mobile = I('post.mobile', '');
        $q_status = I('post.q_status', '');
        $a_status = I('post.a_status', '');

        $list = $this->qaService->questionList($title,$cat_ids,$user_name,$mobile,$q_status,$a_status,1);
        $page = $list['data'];

        $busConductList = $this->qaService->questionList($title,$cat_ids,$user_name,$mobile,$q_status,$a_status,0,$nowPage);

        $this->assign('page', $page->show());
        $this->assign('lists', $busConductList['data']);
        $this->display();
    }



    public function detail() {
        $id = I('get.id');
        $info = $this->qaService->questionInfo($id);

        $users=M('users')->where('is_lock = 0')->field('user_id,username,mobile')->select();
        if($info['no_answer']){
            $account_users=M('accountant_users')->where('status = 1')->field('au_id,username,mobile')->select();
        }

        if($info['has_answer'] &&  $info['has_answer']['charge_view']){
            $info['has_answer']['count'] = M('pay_log')->where('question_id = '.$id.' and type = 3 and status = 1')->count();
        }
        $cat_ids = M('categories')->getField('id,cat_name');
        $this->assign('cat_ids',$cat_ids);
        $this->assign('users', $users);
        $this->assign('info', $info['info']);
        $this->assign('has_answer', $info['has_answer']);
        $this->assign('no_answer', $info['no_answer']);
        $this->assign('account_users', $account_users);
        $this->assign('act','edit');
        $this->display();
    }


    public function add() {
        $this->assign('act','add');
        $this->display();
    }

    public function handle() {
        if (IS_POST){

            $data = I('post.');
            $res = $this->qaService->handle($data);

            if ($res['code'] == 200 && $data['act'] != 'del'){

                $this->success('操作成功', U('Admin/qa/index'));
            } elseif($res['code'] != 200 && $data['act'] != 'del'){

                $this->error('操作失败');
            } elseif ($data['act'] == 'del'){

                $this->ajaxReturn($res);
            }
        }
    }


}
